package edu.upc.fib.lamsandroid.client.activity;

import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import edu.upc.fib.lamsandroid.client.LamsAndroidClientApplication;
import edu.upc.fib.lamsandroid.client.R;
import edu.upc.fib.lamsandroid.client.dialog.ConfirmBoxDialog;

import java.util.List;

public class ConfigurationActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(hasHeaders()) {
            Button button = new Button(this);
            button.setText("Restore defaults");
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new ConfirmBoxDialog(ConfigurationActivity.this, new ConfirmBoxDialog.ConfirmBoxDialogListener() {
                        @Override
                        public void onAccept() {
                            LamsAndroidClientApplication.getInstance().resetPreferences();
                            finish();
                        }

                        @Override
                        public void onDeny() {
                            // nothing
                        }
                    }).show("Reset all configuration parameters?");
                }
            });
            setListFooter(button);
        }
    }

    @Override
    public void onBuildHeaders(List<Header> target) {
        loadHeadersFromResource(R.xml.preference_headers, target);
    }

    public static class ConnectionPrefsFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Make sure default values are applied.  In a real app, you would
            // want this in a shared function that is used to retrieve the
            // SharedPreferences wherever they are needed.
            PreferenceManager.setDefaultValues(getActivity(),
                    R.xml.prefs_connection, false);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.prefs_connection);
        }
    }
}
